<template>
  <div class="list">
    <div class="content-head" style="margin-bottom: 20px;">
      <div class="sort-keys">
        <a-input placeholder="输入用户手机号" style="width: 180px;margin-right:10px;" v-model="search_phone" allowClear :size="size" @input="onSearch" />
        <span style="margin-left: 20px; margin-right: 10px;">排序</span>
        <a-select style="width: 200px;margin-right:10px;" v-model="orderBy" placeholder="排序" allowClear>
          <a-select-option :value="item.value" v-for="item in orderByList" :key="item.value">{{ item.title }}</a-select-option>
        </a-select>
      </div>
    </div>
    <a-table class="table" :rowKey="row => row.id" :dataSource="list" :pagination="pagination" :size="size">

      <a-table-column title="用户" width="140px">
        <template slot-scope="row">
          <div class="user" v-if="row.id">
            <img :src="row.user.headimg" alt="用户头像">
            <div>
              <div>{{row.user.name}}</div>
              <div>{{row.phone}}</div>
            </div>
          </div>
        </template>
      </a-table-column>
      
      <a-table-column title="用户标签" width="80px" >
        <template slot-scope="row">
            <a-tag color="red" style="margin-left:10px;" v-if="row.user.status === 2">已拉黑</a-tag>
            <a-tag color="red" style="margin-left:10px;" v-if="row.user.is_rb === 1">马甲</a-tag>
            <a-tag color="red" style="margin-left:10px;" v-if="row.user.is_rb2 === 1">主播</a-tag>
            <a-tag color="red" style="margin-left:10px;" v-if="row.user.is_liu === 1">老六</a-tag>
            <a-tag color="red" style="margin-left:10px;" v-if="row.user.is_nopay === 1">禁付</a-tag>
            <a-tag color="blue" style="margin-left:10px;" v-if="row.user.click_id">微广</a-tag>
        </template>
      </a-table-column>
      
      <a-table-column title="微信群" width="80px" >
        <template slot-scope="row">
          <a-tag style="margin-left:10px;" v-for="(item, index) in row.qy_groups">{{item}}</a-tag>
        </template>
      </a-table-column>
      
      <a-table-column title="注册日期" width="110px">
        <template slot-scope="row">
          <span :style="row.user.created_at | parseStyle">{{ row.user.created_at | formatDate }}</span>
        </template>
      </a-table-column>
    
      <a-table-column title="实付总金额" width="100px">
        <template slot-scope="row">
          <NumberGroupDisplay :info="row" prefix="total_pay_"></NumberGroupDisplay>
        </template>
      </a-table-column>
      <a-table-column title="总亏损金额" width="100px">
        <template slot-scope="row">
          <NumberGroupDisplay :info="row" prefix="total_loss_" :style="{ color: row.total_loss_money_price < 0 ? 'red' : 'inherit' }"></NumberGroupDisplay>
        </template>
      </a-table-column>
      <a-table-column title="注册天数" width="110px">
        <template slot-scope="row">
          <span>{{ row.register_day_num }}</span>
        </template>
      </a-table-column>
      <a-table-column title="活跃天数" width="110px">
        <template slot-scope="row">
          <span>{{ row.active_day_num }}</span>
        </template>
      </a-table-column>
      <a-table-column title="实付天数" width="110px">
        <template slot-scope="row">
          <span>{{ row.pay_day_num }}</span>
        </template>
      </a-table-column>      
      <a-table-column title="实付天数占比" width="110px">
        <template slot-scope="row">
          <span>{{ row.pay_day_ratio }}%</span>
        </template>
      </a-table-column>
      <a-table-column title="闲时占比" width="110px">
        <template slot-scope="row">
          <span>{{ row.unpay_day_ratio }}%</span>
        </template>
      </a-table-column>
      <a-table-column title="每日平均实付" width="100px">
        <template slot-scope="row">
          <a href="javascript:;" @click="openList(row.user_id, 'today_pay_money_price', row.user)">
          <NumberGroupDisplay :info="row" prefix="avg_per_pay_"></NumberGroupDisplay>
          </a>
        </template>
      </a-table-column>
      <a-table-column title="平均每日亏损" width="100px">
        <template slot-scope="row">
          <a href="javascript:;" @click="openList(row.user_id, 'today_loss_money_price', row.user)">
          <NumberGroupDisplay :info="row" prefix="avg_per_loss_" :style="{ color: row.total_loss_money_price < 0 ? 'red' : 'inherit' }"></NumberGroupDisplay>
          </a>
        </template>
      </a-table-column>
    </a-table>
    <TopUserRecord @hide="isTopUserRecord=false" :isShow="isTopUserRecord" :user_info="select_user_info" :search_type="search_type" :user_id="open_user_id" v-if="isTopUserRecord"></TopUserRecord>
  </div>
</template>
<script>
  import moment from 'moment'
  import ISwitch from '@/components/ISwitch/ISwitch'

  import NumberGroupDisplay from './components/NumberGroupDisplay'

  import IDatePicker from "@/components/IDatePicker/IDatePicker.vue"
  import TopUserRecord from "./components/TopUserRecord"

  const resourceType = 'stats/top-user'
  const resourceTypeText = '实付前50名用户消费分析'

  export default {
    components: {
      ISwitch,
      NumberGroupDisplay,
      IDatePicker,
      TopUserRecord
    },
    props: {
      selectMode: {
        type: Boolean
      },
      params: {
        type: Object
      },
      size: {
        type: String,
        default: 'default'
      }
    },
    data() {
      return {
        isEdit: false,
        isTopUserRecord: false,
        user_id: undefined,
        select_user_info: {},
        open_user_id: undefined,
        search_type: 'today_pay_money_price',
        editUuid: '',
        resourceTypeText,
        exportOptions: {
          status: 'all',
          startTime: '',
          endTime: '',
          type: 'normal'
        },
        list: [],
        page: 1,
        per_page: 10,
        total: 0,
        search_phone: '',
        keyType: 'phone',
        category_id: undefined,
        userType: undefined,
        orderBy: undefined,
        orderByList: [
          {
            title: '按-实付金额-降序',
            value: 'total_pay_money_price_desc',
          },
          {
            title: '按-实付金额-升序',
            value: 'total_pay_money_price_asc',
          },
          {
            title: '按-总亏损金额-降序',
            value: 'total_loss_money_price_desc',
          },
          {
            title: '按-总亏损金额-升序',
            value: 'total_loss_money_price_asc',
          },          
          {
            title: '按-活动天数-降序',
            value: 'active_day_num_desc',
          },          
          {
            title: '按-活动天数-升序',
            value: 'active_day_num_asc',
          },
          {
            title: '按-实付天数占比-降序',
            value: 'pay_day_ratio_desc',
          },          
          {
            title: '按-实付天数占比-升序',
            value: 'pay_day_ratio_asc',
          },
          {
            title: '按-每日平均实付-降序',
            value: 'avg_per_pay_money_price_desc',
          },          
          {
            title: '按-每日平均实付-升序',
            value: 'avg_per_pay_money_price_asc',
          },
          {
            title: '按-每日平均亏损-降序',
            value: 'avg_per_loss_money_price_desc',
          },          
          {
            title: '按-每日平均亏损-升序',
            value: 'avg_per_loss_money_price_asc',
          }
        ],              
        filter: {
          min_day: '',
          max_day: ''
        }
      }
    },
    watch: {
      params() {
        this.refreshData()
      },
      category_id() {
        this.refreshData()
      },
      orderBy() {
        this.refreshData()
      }
    },
    computed: {
      pagination() {
        return {
          defaultPageSize: this.per_page,
          current: this.page,
          total: this.total,
          onChange: (e) => {
            this.page = e
            this.fetchData()
          }
        }
      }
    },
    filters: {
      parseStyle (value) {
        if(new Date(value).getTime() >= (new Date().getTime() - 86400*2000) ){
            return `color:red;`
        }
      },
      formatDate (value) {
        return value ? moment(value).fromNow() : ''
        
      }
    },
    created() {
      this.fetchData()
      // this.getCategoryList()
    },
    methods: {
      setTop(row) {
        this.$confirm({
          title: '提示',
          content: `确认要置顶此帖子吗？`,
          onOk: () => {
            this.$http.put(`/${resourceType}/${row.uuid}`, {
              type: 'set_top'
            }).then(res => {
              this.fetchData()
            })
          }
        })
      },
      unsetTop(row) {
        this.$confirm({
          title: '提示',
          content: `确认要取消置顶此帖子吗？`,
          onOk: () => {
            this.$http.put(`/${resourceType}/${row.uuid}`, {
              type: 'unset_top'
            }).then(res => {
              this.fetchData()
            })
          }
        })
      },
      handleDelete(row) {
        this.$confirm({
          title: '提示',
          content: `确认删除吗？`,
          onOk: () => {
            this.$http.delete(`/${resourceType}/${row.uuid}`).then(res => {
              this.fetchData()
            })
          }
        })
      },
      updateItem(row, data) {
        return this.$http.patch(`/${resourceType}/${row.uuid}`, {
          type: 'update',
          attributes: {
            ...data
          }
        })
      },
      recommendStatusChange(e, row) {
        this.updateItem(row, {
          is_recommend: e
        }).then(res => {
          this.fetchData()
        })
      },
      listedStatusChange(e, row) {
        this.updateItem(row, {
          status: e
        }).then(res => {
          this.fetchData()
        })
      },
      handleEditListWeight(row) {
        this.$globalForm.show({
          title: '编辑排序',
          items: [{
            type: 'number',
            key: 'list_weight',
            title: '排序',
            default: row.list_weight,
            required: true
          }, ],
          success: (data) => {
            this.updateItem(row, {
              ...data
            }).then(res => {
              this.$message.success('保存成功');
              this.$globalForm.hide()
              this.fetchData()
            })
          }
        })
      },
      handleCreate() {
        this.editUuid = ""
        this.isEdit = true
      },
      handleEdit(row) {
        this.editUuid = row.uuid
        this.isEdit = true
      },
      refreshData() {
        this.page = 1
        // this.key = ''
        this.fetchData()
      },
      onSearch(e) {
        this.page = 1
        this.fetchData()
      },
      fetchData() {
        this.isEdit = false
        this.$http.get(`/${resourceType}`, {
          params: {
            ...this.params,
            page: this.page,
            per_page: this.per_page,
            search_phone: this.search_phone,
            order_by_field: this.orderBy
          }
        }).then(res => {
          this.list = res.data.list
          this.total = res.data.item_total
        }).finally(() => {
          this.loading = false
        })
      },
      openList(user_id, search_type, user_info) {
        this.open_user_id = user_id
        this.search_type = search_type
        this.select_user_info = user_info
        this.isTopUserRecord = !this.isTopUserRecord
      },
      getCategoryList() {
        this.$api.emit('category.list', 'article').then(res => {
          this.category = res.data.list
        })
      },
    }
  }
</script>
<style lang="scss" scoped>
  .top-btn {
    background: #1890FF;
    color: white;
    cursor: pointer;
    font-size: 80%;
    border-radius: 4px;
    padding: 2px 6px;

    &.unset {
      border: 1px solid #D8D5D5;
      background: #D8D5D5;
      color: gray;
    }
  }

  .content-head {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
  }

  .table {
    .thumb {
      height: 60px;
      border-radius: 6px;
    }

    .content-image {
      height: 60px;
      width: 60px;
      margin-right: 10px;
      margin-bottom: 10px;
      object-fit: cover;
      border-radius: 10px;
      cursor: pointer;

      &:hover {
        opacity: 0.8;
      }
    }

    .user {
      display: flex;
      // flex-wrap: wrap;
      align-items: center;

      img {
        width: 26px;
        height: 26px;
        margin-right: 6px;
        border-radius: 50%;
        overflow: hidden;
      }

      .gender-man,
      .gender-woman {
        margin-left: 5px;
        font-size: 12px;
      }

      .gender-man {
        color: #409EFF;
      }

      .gender-woman {
        color: #F56C6C;
      }
    }
  }
</style>
